package com.gqz.equip.app.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.gqz.common.core.domain.RequestForm;
import com.gqz.common.core.utils.poi.ExcelUtil;
import com.gqz.common.core.utils.query.QueryMapperUtil;
import com.gqz.equip.app.entity.EnWzJfEquipEntity;
import com.gqz.equip.app.enumEntity.QyEnum;
import com.gqz.equip.app.mapper.EquipEnWzJfEquipMapper;
import com.gqz.equip.app.mapstruct.EquipEnWzJfEquipMapstruct;
import com.gqz.equip.app.service.IEquipEnWzJfEquipService;
import com.gqz.equip.vo.EnWzJfEquipVo;
import com.gqz.equip.vo.WzEquipBylxVo;
import com.gqz.equip.vo.WzEquipTjVo;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 新能源设备信息 业务逻辑实现层ServiceImpl
 *
 * @Author input you name
 */
@Service
@Slf4j
public class EquipEnWzJfEquipServiceImpl extends ServiceImpl<EquipEnWzJfEquipMapper, EnWzJfEquipEntity> implements IEquipEnWzJfEquipService {

    @Autowired
    private EquipEnWzJfEquipMapstruct mapstruct;

    @Override
    public List<EnWzJfEquipVo> getById(String id) {
        MPJLambdaWrapper<EnWzJfEquipEntity> queryWrapper = new MPJLambdaWrapper<>();
        /** todo getPsrId-> todo-must修改为记录表主键字段todo */
        queryWrapper.eq(EnWzJfEquipEntity::getSbbm, id);
        queryWrapper.selectAll(EnWzJfEquipEntity.class);
        List<EnWzJfEquipEntity> entities = baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(entities)) {
            return mapstruct.toVos(entities);
        }
        return new ArrayList<>();
    }

    @Override
    public Page<EnWzJfEquipVo> page(RequestForm<EnWzJfEquipVo> requestForm) {
        EnWzJfEquipEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<EnWzJfEquipEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        queryWrapper.selectAll(EnWzJfEquipEntity.class);
        queryWrapper.eq(EnWzJfEquipEntity::getSfsc, 0);
        queryWrapper.orderByDesc(EnWzJfEquipEntity::getCjtime);
        //&& !"BJ-EN-001".equals(requestForm.getEntity().getGldwbm())
        if (requestForm.getEntity().getGldwbm() != null && !"".equals(requestForm.getEntity().getGldwbm())) {
            queryWrapper.eq(EnWzJfEquipEntity::getGldwbm, requestForm.getEntity().getGldwbm());
        }
        if (requestForm.getEntity().getSblxbm() != null && !"".equals(requestForm.getEntity().getSblxbm())) {
            queryWrapper.eq(EnWzJfEquipEntity::getSblxbm, requestForm.getEntity().getSblxbm());
        }
        if (requestForm.getEntity().getSbmc() != null && !"".equals(requestForm.getEntity().getSbmc())) {
            queryWrapper.like(EnWzJfEquipEntity::getSbmc, requestForm.getEntity().getSbmc());
        }
        if (requestForm.getEntity().getCcbm() != null && !"".equals(requestForm.getEntity().getCcbm())) {
            queryWrapper.like(EnWzJfEquipEntity::getCcbm, requestForm.getEntity().getCcbm());
        }
        if (requestForm.getEntity().getSsjfbm() != null && !"".equals(requestForm.getEntity().getSsjfbm())) {
            queryWrapper.eq(EnWzJfEquipEntity::getSsjfbm, requestForm.getEntity().getSsjfbm());
        }
        if (requestForm.getEntity().getYxzt() != null && !"".equals(requestForm.getEntity().getYxzt())) {
            queryWrapper.eq(EnWzJfEquipEntity::getYxzt, requestForm.getEntity().getYxzt());
        }
        if (requestForm.getEntity().getSfqy() != null && !"".equals(requestForm.getEntity().getSfqy())) {
            queryWrapper.eq(EnWzJfEquipEntity::getSfqy, requestForm.getEntity().getSfqy());
        }
        if (requestForm.getEntity().getCcrq() != null && !"".equals(requestForm.getEntity().getCcrq())) {
            queryWrapper.ge(EnWzJfEquipEntity::getCcrq, requestForm.getEntity().getCcrq());
        }
        if (requestForm.getEntity().getCcrq_to() != null && !"".equals(requestForm.getEntity().getCcrq_to())) {
            queryWrapper.le(EnWzJfEquipEntity::getCcrq, requestForm.getEntity().getCcrq_to());
        }
        Page<EnWzJfEquipEntity> page = Page.of(requestForm.getPageNo(), requestForm.getPageSize());
        //page = baseMapper.selectPage(page, queryWrapper);
        IPage<EnWzJfEquipEntity> iPage = baseMapper.selectJoinPage(page, EnWzJfEquipEntity.class, queryWrapper);
        List<EnWzJfEquipEntity> retestVoList = iPage.getRecords().stream().map(item -> {
            item.setSfqy(QyEnum.getValue(item.getSfqy()));
            return item;
        }).collect(Collectors.toList());
        page.setRecords(iPage.getRecords());
        page.setTotal(iPage.getTotal());
        return mapstruct.toVos(page);
    }

    @Override
    public Long count(RequestForm<EnWzJfEquipVo> requestForm) {
        EnWzJfEquipEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<EnWzJfEquipEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        /** todo getPsrId-> todo-must修改为记录表主键字段todo */
        queryWrapper.select(EnWzJfEquipEntity::getSbbm);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public int insert(List<EnWzJfEquipVo> forms) {
        List<EnWzJfEquipEntity> entitys = mapstruct.toEntitys(forms);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            EnWzJfEquipEntity entity = entitys.get(i);
            result += baseMapper.insert(entity);
        }
        return result;
    }

    @Override
    public int update(List<EnWzJfEquipVo> forms) {
        List<EnWzJfEquipEntity> entitys = mapstruct.toEntitys(forms);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            EnWzJfEquipEntity entity = entitys.get(i);
            result += baseMapper.updateById(entity);
        }
        return result;
    }

    @SneakyThrows
    @Override
    public void exportExcel(HttpServletResponse response, RequestForm<EnWzJfEquipVo> requestForm) {
        //设置信息头，告诉浏览器内容为excel类型
        response.setHeader("content-Type", "application/vnd.ms-excel");
        //文件名称
        String fileName = "新能源设备信息信息.xls";
        //sheet名称
        String sheetName = "新能源设备信息列表";
        //设置下载名称
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        //获取到业务数�?
        Page<EnWzJfEquipVo> page = this.page(requestForm);
        //导入excel
        ExcelUtil<EnWzJfEquipVo> util = new ExcelUtil<>(EnWzJfEquipVo.class);
        util.exportExcel(response, page.getRecords(), sheetName);

    }

    @Override
    public WzEquipTjVo jfEquipTj(EnWzJfEquipVo vos) {
        //各类型数量
        MPJLambdaWrapper<EnWzJfEquipEntity> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(EnWzJfEquipEntity::getSfsc, "0");
        queryWrapper.select(EnWzJfEquipEntity::getSblxmc);
        queryWrapper.select(EnWzJfEquipEntity::getSblxbm);
        queryWrapper.selectCount(EnWzJfEquipEntity::getSblxbm, "equipNum");
        queryWrapper.groupBy(EnWzJfEquipEntity::getSblxbm);
        queryWrapper.groupBy(EnWzJfEquipEntity::getSblxmc);
        if (vos.getGldwbm() != null && !"BJ-EN-001".equals(vos.getGldwbm()) && !"".equals(vos.getGldwbm())) {
            queryWrapper.like(EnWzJfEquipEntity::getGldwbm, vos.getGldwbm());
        }
//        List<TEnWzEquipEntity> entities = baseMapper.selectList(queryWrapper);
        List<WzEquipBylxVo> resultList = baseMapper.selectJoinList(WzEquipBylxVo.class, queryWrapper);
        WzEquipTjVo jfEquipTjVo = new WzEquipTjVo();
        jfEquipTjVo.setWzEquipList(resultList);
        //总数
        int equipsum = 0;
        for (int i = 0; i < resultList.size(); i++) {
            equipsum += resultList.get(i).getEquipNum();
        }
        jfEquipTjVo.setAllNum(equipsum);
        return jfEquipTjVo;
    }


}
